import api from "../../api/index";

const app = getApp()

Component({
    properties: {
        showModalNickname: {
            type: Boolean,
            default: false
        },

        userInfoNew: {
            type: Object,
            value: {}
        }
    },

    methods: {
        saveNickname(e) {
            if(this.data.userInfoNew.nickName && this.data.userInfoNew.nickName !== '微信用户') {
                // console.log('guanbi', e);
                // console.log(this.data.userInfoNew.nickName);
                this.setData({
                    'showModalNickname': false,
                    'userInfoNew.nickName': this.data.userInfoNew.nickName
                })
                this.saveUserInfo({
                    nickname: this.data.userInfoNew.nickName
                }, false).then(() => {
                    app.getHttpUser().then(() => {
                        this.triggerEvent('nickname-changed', this.data.userInfoNew.nickName)
                    })
                })
            } else if(!this.data.userInfoNew.nickName || this.data.userInfoNew.nickName == '微信用户'){
                wx.showToast({
                    title: '请输入合法的昵称！',
                    icon: 'none',
                    duration: 2000
                })
            }
        },

        userNameInput(e) {
            // console.log(e.detail.value)
            this.setData({
                'userInfoNew.nickName': e.detail.value,
            });
        },

        saveUserInfo(data = {}) {
            return new Promise((resolve, reject) => {
                api.post('/api/user/savenickname', {
                    data: data,
                }).then(res => {
                    resolve(res)
                }).catch(e => {
                    reject(e)
                })
            })
        },

        closeSaveNickname() {
            this.setData({
                showModalNickname: false
            })
        }
    }
})